package com.zllkj.mm.module.shopping.fragment

import android.view.KeyEvent
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zllkj.mm.R
import com.zllkj.mm.adapter.NearByShopGoodsListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.common.ClientConfig
import com.zllkj.mm.model.entity.GoodsVo
import com.zllkj.mm.model.entity.NearByGoodsListBean
import com.zllkj.mm.model.entity.OrderDetailBean
import com.zllkj.mm.model.entity.ShopDetailVo
import com.zllkj.mm.module.goods.GoodsDetailFragment
import com.zllkj.mm.module.goods.vm.ItemNearByShopGoodsVM
import com.zllkj.mm.util.StaggeredDividerItemDecoration
import com.zllkj.mm.util.common.FormatUtil
import com.zllkj.mm.util.common.PictureLoadUtil
import kotlinx.android.synthetic.main.activity_order_detail.*
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import kotlinx.android.synthetic.main.fragment_shop_detail.*
import kotlinx.android.synthetic.main.fragment_shop_detail.view.*
import java.math.BigDecimal
import java.util.HashMap

class ShopDetailFragment(val shopId:String):BaseTitleFragment() {


    var currentPage = 1


    var datas = ArrayList<ItemNearByShopGoodsVM>()
    lateinit var adapter : NearByShopGoodsListAdapter

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_shop_detail;
    }

    fun loadListData(keyword:String){


        addSubscribe(RetrofitHelper.getInstance().goodsApi.getGoodsOnShelfList((HashMap<String, Any>().apply {
            this["currentPage"] = 1
            this["pageSize"] = 20
            this["deliveryMode"] = 0
            this["goodsClassifyId"] = 0
            this["goodsName"] = keyword
            this["isStock"] = 0
            this["shopId"] = shopId
        })).compose<NearByGoodsListBean>(RxHelper.defaultHandleResult<NearByGoodsListBean>()).subscribe(object : ProgressSubscriber<NearByGoodsListBean>(requireContext()){
            override fun onSucc(t: NearByGoodsListBean?) {
                t?.records?.let {
                    it.map {
                        var vm = ItemNearByShopGoodsVM()
                        vm.unitPrice = FormatUtil.bigDecimalUtilFormat(it.unitPrice.toString(),"100")
                        vm.shopName = it.shopName
                        vm.goodsName = it.goodsName
                        vm.saleCount = it.sales.toString()
                        vm.picUrl = it.goodsPic
                        vm.shopId = it.shopId.toString()
                        vm.goodsId = it.goodsId.toString()
                        vm.isAd = it.advertising == 1
                        datas.add(vm)
                    }
                    adapter.notifyDataSetChanged()
                }


            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

    override fun loadData() {
        super.loadData()

        setCommonTitle("店铺详情")

        mRootView.rv_mycollection.layoutManager = GridLayoutManager(requireContext(),2)
        adapter = NearByShopGoodsListAdapter(R.layout.item_near_by_shop_goods_list,datas)
        mRootView.rv_mycollection.adapter = adapter
        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            addFragment(GoodsDetailFragment(datas[i].goodsId))

        }

        addSubscribe(RetrofitHelper.getInstance().shopApi.getShopDetail((HashMap<String, Any>().apply {
            this["shopId"] = shopId
        })).compose<ShopDetailVo>(RxHelper.defaultHandleResult<ShopDetailVo>()).subscribe(object : ProgressSubscriber<ShopDetailVo>(requireContext()){
            override fun onSucc(t: ShopDetailVo?) {
                t?.let {
                     PictureLoadUtil.loadPic(mRootView.iv_shop_detail_userIcon,it.shopLogo)
                     mRootView.tv_shop_detail_money_fans_content.text = t?.fansNum.toString()
                     mRootView.tv_shop_detail_money_brand_content.text = t?.businessScope.toString()
                     mRootView.tv_shop_detail_money_content.text = FormatUtil.bigDecimalUtilFormat(t?.securityDeposit.toString(),"100").replace("¥","")
                     mRootView.tv_shop_detail_shopName.text = t?.shopName

                    if (it.fansType == 0) {
                        mRootView.tv_follow_status.text = "关注"
                    } else if (it.fansType == 1) {
                        mRootView.tv_follow_status.text = "已关注"
                    }
                }

            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

        loadListData("")

        mRootView.ed_goods_list_search.setOnEditorActionListener { v, actionId, event ->
            if (event.action == KeyEvent.ACTION_DOWN){
                loadListData(mRootView.ed_goods_list_search.text.toString())
            }
            true
        }

        mRootView.tv_follow_status.setOnClickListener {

        }


        mRootView.tv_follow_status.setOnClickListener {

        }

    }
}